package com.gzx.spring.drools.job;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gzx.risk.core.service.NotifyService;
import com.gzx.spring.drools.service.RiskWorkService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

@Service
@Slf4j
public class DroolsJob {
	@Resource
	private RiskWorkService riskWorkService;
	@Resource
	private NotifyService notifyService;

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final Set<String> SPECIAL_DATES = new HashSet<>() {{
        add("2025-11-23");
        add("2025-11-24");
        add("2025-11-25");
        add("2025-11-26");
        add("2025-11-27");
    }};

	/**
	 * 定时任务<br>
	 * 超过4小时订单，自动进入风控
	 * */
	@Scheduled(fixedDelay = 60000)
	public void checkOrders() {
		log.info("checkOrders start");
		riskWorkService.exec();
	}

    /**
     * 定时任务<br>
     * 执行风控二阶段
     * */
    @Scheduled(fixedDelay = 90000)
    public void checkOrdersTwo() {
        log.info("checkOrdersTwo start");
        riskWorkService.execRiskTwo();
    }

	// https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=cbfb1bd4-c617-45e1-b5bb-25e3f6e98ab7
	/**
	 * 通知机器人 <br>
	 * 1、待付款<br>
	 * 2、待风控<br>
	 */
	@Scheduled(fixedDelay = 60000)
	public void checkOrderToNotify() throws Exception {
		log.info("checkOrderToNotify start");
		notifyService.notifyOrders();
	}

	// 每天9点和21点执行
	// 发送
//	@Scheduled(cron = "0 0 9,21 * * ?")
	public void notifyReport() throws Exception {
        String currentDate = LocalDate.now().format(DATE_FORMATTER);
        if (SPECIAL_DATES.contains(currentDate)) {
            return;
        }
		notifyService.notifyReport();
	}

    /**
     * 只发测试群（战略数据群）
     * @throws Exception
     */
    @Scheduled(cron = "0 0 9 * * ?")
    public void notifyReportTemp() throws Exception {
        String currentDate = LocalDate.now().format(DATE_FORMATTER);
        if (!SPECIAL_DATES.contains(currentDate)) {
            return;
        }
        notifyService.notifyReportTemp(currentDate);
    }
}
